package com.ysx.teacher.day20210910.sm;

import java.util.ArrayList;

public class StudentDao {

    private ArrayList<Student> arrayList= new ArrayList<Student>();

    public boolean addStudent(Student student){
        arrayList.add(student);
        return true;
    }

    public boolean login(String name,String password){

        // 1 遍历arrayList
        for(int index=0;index<arrayList.size();index++){
            // 2 拿到1个Student对象
            Student student =arrayList.get(index);

            // 3 拿到对象的userName和password属性
            String un = student.getUserName();
            String pw =student.getPassword();
            // 4 判断这2个属性是否都相等 true false
            if(un.equals(name) && pw.equals(password)){
                return true;
            }

        }
        return false;
    }

    public  ArrayList<Student> findAllStudent(){
        return arrayList;

    }

    public  int findMaxMaths(){

        int max=0;
        // 1 遍历arrayList
        for(int index=0;index<arrayList.size();index++){
            // 2 拿到1个Student对象
            Student student = arrayList.get(index);
            // 3 拿到对象的maths属性
            int maths = student.getMaths();
            // 4 默认第1个就是数学最高分  如果后面的比它大 就替换 否则不变
            if(maths>max){
                max=maths;
            }
        }

        return max;

    }

    public  double findMaxAverage(){

        double max=0;
        // 1 遍历arrayList
        for(int index=0;index<arrayList.size();index++){

            // 2 拿到1个Student对象
            Student student = arrayList.get(index);

            // 3 拿到对象的maths、english、chinese属性  /3
            int maths   = student.getMaths();
            int english = student.getEnglish();
            int chinese = student.getChinese();

            double avg= (maths+english+chinese)/3;
            // 4 默认第1个就是平均分最高分  如果后面的平均分比它大 就替换 否则不变
            if(avg>max){
                max=avg;
            }
        }
        return max;

    }

    public  void orderByAverage(){


        // 1 遍历arrayList
        int begin=0;
        for(int ounter=begin;ounter<arrayList.size();ounter++){
            // 2 拿到1个Student对象
            Student first = arrayList.get(ounter);
            // 3 拿到对象的maths、english、chinese属性  /3
            int maths   = first.getMaths();
            int english = first.getEnglish();
            int chinese = first.getChinese();
            double avg= (maths+english+chinese)/3;
            for(int inner=begin+1;inner<arrayList.size();inner++){
                // 2 拿到1个Student对象
                Student second = arrayList.get(inner);
                // 3 拿到对象的maths、english、chinese属性  /3
                int maths2   = second.getMaths();
                int english2 = second.getEnglish();
                int chinese2 = second.getChinese();
                double avg2= (maths2+english2+chinese2)/3;
                // 4 把sum和后面的成绩分别比较 如果比后面的大 交换位置 否则不变
                if(avg>avg2){
                    // 互换位置
                    Student temp=null;
                    temp=first;
                    first=second;
                    arrayList.set(ounter,first);
                    second=temp;
                    arrayList.set(inner,second);
                    System.out.println("---");

                }

            }

        }
    }
    public  boolean updateMathsByUserName(String userName,int newScore){
        // 1 遍历arrayList
        for(int index=0;index<arrayList.size();index++){
            // 2 拿到1个Student对象
            Student student = arrayList.get(index);

            // 3 如果userName 就用newScore代替原来的成绩
            String un = student.getUserName();
            if(un.equals(userName)){
                //设置成绩
                student.setMaths(newScore);
            }


        }

        return true;

    }

    public  boolean deleteStudent(String userName){

        // 1 遍历arrayList
        for(int index=0;index<arrayList.size();index++){

            // 2 拿到1个Student对象
            Student student = arrayList.get(index);
            String un = student.getUserName();
            if(un.equals(userName)){
                arrayList.remove(index);
                return  true;
            }
        }
        return false;

    }


}
